package com.Controller.Struts.Action;

import com.Controller.Struts.ModelDriven.RegisterUser;
import com.Model.Date.DateTime;
import com.Model.Pojo.Users;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sql.DAO;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpSession;
import java.util.Random;

public class RegisterAction extends ActionSupport implements ModelDriven<RegisterUser> {

    private RegisterUser registerUser = new RegisterUser();
    @Override
    public RegisterUser getModel() {
        return registerUser;
    }

    public String execute(){
        String registerResult;
        HttpSession session = ServletActionContext.getRequest().getSession();
        Random rand = new Random();
        int zh = rand.nextInt(90000) + 10001;
        String zhanghao = zh + "";
        String name = registerUser.getName();
        String email = registerUser.getEmail();
        String shoujihao = registerUser.getShoujihao();
        String pass1 = registerUser.getPassword();
        int yue = 100;
        int money = 0;
        Users user = new Users();
        pass1 = pass1 + "1";
        String sql = "insert into users values('" + zhanghao + "','"
                + pass1 + "','" + yue + "" + "','" + name + "','"
                + email + "','" + shoujihao + "','" + money + "')";
        DAO dao = new DAO();
        dao.zengjia(sql);
        user.setZhanghao(zhanghao);
        user.setMima(pass1);
        user.setYue(yue);
        user.setXingming(name);
        user.setYouxiang(email);
        user.setShoujihao(shoujihao);
        user.setMoney(money);
        session.setAttribute("users", user);

        DateTime date = new DateTime();
        sql = "insert into cunkuanqvkuan (name,money,style,date,yue)value('"
                + zhanghao + "','100','1','" + date.getSettime() + "','100')";
        dao.zengjia(sql);

        String usersname = zhanghao;
        String style1 = "存款";
        int cunmoney = 100;
        int cash = 0;
        String source = "开户预存";

        sql = "insert into moneylist (name,style,value,date,cash,source)value('" + usersname + "','" + style1
                + "','" + cunmoney + "','" + date.getSettime() + "','" + cash + "','" + source + "')";
        dao.zengjia(sql);
        registerResult = Action.SUCCESS;
        return registerResult;
    }
}
